/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmTipoConceptos.java
 *
 * Created on 18/10/2012, 01:18:12 AM
 */

package co.edu.unitecnologica.interfaz;

import co.edu.unitecnologica.modelo.TiposConceptos;
import java.util.Calendar;

/**
 *
 * @author osalcedo
 */
public class FrmTipoConceptos extends Formulario {
    

    /** Creates new form FrmTipoConceptos */
    public FrmTipoConceptos(Principal p) {
        super(p);
        initComponents();
        habilitarCampos(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jmiImprimir = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jtfCod = new javax.swing.JTextField();
        jtfDescripcion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        jmiImprimir.setText("Imprimir");

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Tipos de conceptos");

        jLabel1.setText("Codigo:");

        jLabel2.setText("Descripcion:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfDescripcion)
                    .addComponent(jtfCod, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))
                .addGap(22, 22, 22))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JMenuItem jmiImprimir;
    private javax.swing.JTextField jtfCod;
    private javax.swing.JTextField jtfDescripcion;
    // End of variables declaration//GEN-END:variables

    @Override
    public boolean insertarRegistro() {
        try{
            em.getTransaction().begin();
            TiposConceptos t= new TiposConceptos();
            t.setCod(Integer.parseInt(jtfCod.getText()));
            t.setDescripcion(jtfDescripcion.getText());
            t.setFechaActividad(Calendar.getInstance().getTime());
            t.setHoraActividad(Calendar.getInstance().getTime());
            t.setUsuario("usuario");
            em.persist(t);
            em.getTransaction().commit();            
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean buscarRegistros() {
        try{
            String sql="SELECT t.* "
                    + " FROM tipos_conceptos t"
                    + " WHERE "
                    + "     t.cod like '%"+jtfCod.getText()+"%' "
                    + "     and upper(t.descripcion) like upper('%"+jtfDescripcion.getText()+"%') ";
            query = em.createNativeQuery(sql,TiposConceptos.class);
            lista= query.getResultList();
            this.setRegistroActual(0);
            this.setRegistrosTotales(lista.size());
            this.mostrarRegistro(0);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public boolean editarRegistro() {
        try{
            em.getTransaction().begin();
            TiposConceptos t= (TiposConceptos) lista.get(getRegistroActual());
            t.setCod(Integer.parseInt(jtfCod.getText()));
            t.setDescripcion(jtfDescripcion.getText());
            t.setFechaActividad(Calendar.getInstance().getTime());
            t.setHoraActividad(Calendar.getInstance().getTime());
            t.setUsuario("usuario");
            em.getTransaction().commit();            
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean borrarRegistro() {
        try{
            em.getTransaction().begin();
            TiposConceptos t= (TiposConceptos) lista.get(getRegistroActual());
            em.remove(t);
            em.getTransaction().commit();            
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public void mostrarRegistro(int r) {
        if(lista.size()>0 && r<lista.size()){
            TiposConceptos t=(TiposConceptos) lista.get(r);
            jtfCod.setText(t.getCod().toString());
            jtfDescripcion.setText(t.getDescripcion());
        }
    }

    @Override
    public void cargarRegistros() {
        query=em.createNamedQuery("TiposConceptos.findAll");
        lista=query.getResultList();
        setRegistroActual(0);
        setRegistrosTotales(lista.size());
        mostrarRegistro(getRegistroActual());
    }

    @Override
    public void limpiarCampos() {
        this.jtfCod.setText("");
        this.jtfDescripcion.setText("");
    }

    @Override
    public final void habilitarCampos(boolean v) {
        this.jtfCod.setEditable(v);
        this.jtfDescripcion.setEditable(v);
    }

    @Override
    public void agregarItemsMenuOpciones() {
        menuOpciones.add(jmiImprimir);
    }

}
